import queue
import tkinter as tk


class View():
    def __init__(self, root):
        self.root = root  # 主窗口
        self.labels = []
        self.bottons = []
        self.txtBoxes = []
        self.data = None
        self.graph = []
        self.t = 0

    def add_data(self, d):
        self.data = d

    def initWindow(self, title, geometry):
        self.root.title(title)
        self.root.geometry(geometry)

    def update(self):
        label = self.labels[0]['label']
        func = self.labels[0]['function']
        if self.data.mode == 1:
            self.t += 1
        if self.t == 7:
            self.t = 0
            self.data.timer += 1
        func(label, self.data.timer)
        label = self.labels[1]['label']
        func = self.labels[1]['function']
        func(label, self.data.heart_rate)
        label = self.labels[2]['label']
        func = self.labels[2]['function']
        func(label, self.data.hypertension)
        label = self.labels[3]['label']
        func = self.labels[3]['function']
        func(label, self.data.hypotension)
        self.root.after(100, self.update)

    def loop(self):
        self.update()
        self.root.mainloop()

    def addLabel(self, text, color, font, width, height, id, updataFun, row, column):
        label = tk.Label(self.root, text=text, bg=color, font=font, width=width, height=height)
        label.place(x=row, y=column)
        self.labels.append({'label': label, 'id': id, 'function': updataFun})  # 绑定标签和id，用于更新数据

    def addButton(self, text, color, font, width, height, id, row, column, commend):
        button = tk.Button(self.root, text=text, bg=color, font=font, width=width, height=height,
                           command=commend)
        button.place(x=row, y=column)
        self.bottons.append({'label': button, 'id': id})

    def addTxtBox(self, text,width, height, id, row, column):
        textBox = tk.Text(self.root, font=text, width=width, height=height)
        textBox.place(x=row, y=column)
        self.txtBoxes.append({'text': textBox, 'id': id})

    def add_graph(self, g):
        self.graph.append(g)
